/**
 * teacher
 */
package com.thinkgem.jeesite.zmis.teacher.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.zmis.teacher.entity.ZmisTeacher;
import com.thinkgem.jeesite.zmis.teacher.service.ZmisTeacherService;

/**
 * 教师Controller
 * @author ll
 * @version 2017-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/teacher/zmisTeacher")
public class ZmisTeacherController extends BaseController {

	@Autowired
	private ZmisTeacherService zmisTeacherService;
	
	@ModelAttribute
	public ZmisTeacher get(@RequestParam(required=false) String id) {
		ZmisTeacher entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = zmisTeacherService.get(id);
		}
		if (entity == null){
			entity = new ZmisTeacher();
		}
		return entity;
	}
	
	@RequiresPermissions("teacher:zmisTeacher:view")
	@RequestMapping(value = {"list", ""})
	public String list(ZmisTeacher zmisTeacher, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ZmisTeacher> page = zmisTeacherService.findPage(new Page<ZmisTeacher>(request, response), zmisTeacher); 
		model.addAttribute("page", page);
		return "zmis/teacher/zmisTeacherList";
	}

	@RequiresPermissions("teacher:zmisTeacher:view")
	@RequestMapping(value = "form")
	public String form(ZmisTeacher zmisTeacher, Model model) {
		model.addAttribute("zmisTeacher", zmisTeacher);
		return "zmis/teacher/zmisTeacherForm";
	}

	@RequiresPermissions("teacher:zmisTeacher:edit")
	@RequestMapping(value = "save")
	public String save(ZmisTeacher zmisTeacher, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, zmisTeacher)){
			return form(zmisTeacher, model);
		}
		zmisTeacherService.save(zmisTeacher);
		addMessage(redirectAttributes, "保存教师成功");
		return "redirect:"+Global.getAdminPath()+"/teacher/zmisTeacher/?repage";
	}
	
	@RequiresPermissions("teacher:zmisTeacher:edit")
	@RequestMapping(value = "delete")
	public String delete(ZmisTeacher zmisTeacher, RedirectAttributes redirectAttributes) {
		zmisTeacherService.delete(zmisTeacher);
		addMessage(redirectAttributes, "删除教师成功");
		return "redirect:"+Global.getAdminPath()+"/teacher/zmisTeacher/?repage";
	}

}